package com.kids.movies.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import com.kids.movies.streaming.MainActivity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Util {
    static public String customFormat(String pattern, double value) {
	DecimalFormat myFormatter = new DecimalFormat(pattern);
	String output = myFormatter.format(value);
	System.out.println(value + "  " + pattern + "  " + output);

	return output;
    }

    /**
     * isConnectingToInternet
     * 
     * @param _context
     * @return
     */
    public static boolean isConnectingToInternet(Context _context) {
	ConnectivityManager connectivity = (ConnectivityManager) _context
		.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (connectivity != null) {
	    NetworkInfo[] info = connectivity.getAllNetworkInfo();
	    if (info != null)
		for (int i = 0; i < info.length; i++)
		    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
			return true;
		    }
	}
	return false;
    }
    public static String millisToString(long millis) {
        boolean negative = millis < 0;
        millis = java.lang.Math.abs(millis);

        millis /= 1000;
        int sec = (int) (millis % 60);
        millis /= 60;
        int min = (int) (millis % 60);
        millis /= 60;
        int hours = (int) millis;

        String time;
        DecimalFormat format = (DecimalFormat)NumberFormat.getInstance(Locale.US);
        format.applyPattern("00");
        if (millis > 0) {
            time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
        } else {
            time = (negative ? "-" : "") + min + ":" + format.format(sec);
        }
        return time;
    }
    
    public static String buildYoutubeUrl(String sUrl){
    	if (sUrl.toLowerCase().contains("www.youtube.com")) {
			if (sUrl.contains("&")) {
				String youtubeUrl = sUrl.split("&")[0];
				String id = youtubeUrl.split("=")[1];
				sUrl = "http://www.youtube.com/get_video_info?&video_id="
						+ id;
			} else {
				String id = sUrl.split("=")[1];
				sUrl = "http://www.youtube.com/get_video_info?&video_id="
						+ id;
			}
			// sUrl = getUrlVideoRTSP(sUrl);
			try {
				sUrl = MainActivity.calculateYouTubeUrl("37", true, sUrl);
			} catch (Exception e) {
				//tempUrl = getUrlVideoRTSP(sUrl);
				e.printStackTrace();
			}

		}
    	
    	return sUrl;
    }

}
